package org.s64.kaptcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.servlet.KaptchaServlet;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.Properties;

@Configuration
@Import(KaptchaProperties.class)
@ConditionalOnClass(KaptchaServlet.class)
public class KaptchaConfiguration {
    @Autowired
    private KaptchaProperties kaptchaProperties;

    @Bean
    public Producer producer() {
        Properties props = new Properties();
        props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, kaptchaProperties.getCharString());
        props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, kaptchaProperties.getFontColor());
        props.setProperty(Constants.KAPTCHA_NOISE_IMPL, kaptchaProperties.getNoiseImpl());
        props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "" + kaptchaProperties.getCharLength());
        props.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "" + kaptchaProperties.getFontSize());
        props.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "" + kaptchaProperties.getImageHeight());
        props.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "" + kaptchaProperties.getImageWidth());
        DefaultKaptcha producer = new DefaultKaptcha();
        producer.setConfig(new Config(props));
        return producer;
    }
}
